package org.example.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.domain.CarInfo;
import org.example.listener.CarInfoListener;
import org.example.service.CarInfoService;
import org.example.util.Result;
import org.example.util.UploadMinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("carInfo")
public class CarInoController {
    @Autowired
    CarInfoService carInfoService;

    @RequestMapping("minio")
    public Result minio(MultipartFile file){
        String pic = UploadMinioUtils.uploadPic(file);
        return Result.success(pic);
    }
    @RequestMapping("del")
    public Result del(Integer carId){
        boolean b=  carInfoService.del(carId);
        return b?Result.success():Result.fail();
    }
    @RequestMapping("update")
    public Result update(@RequestBody CarInfo carInfo){
        boolean b= carInfoService.update(carInfo);
        return b?Result.success():Result.fail();
    }
    @RequestMapping("list")
    public Result list(@RequestBody CarInfo carInfo, @RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<CarInfo> list=carInfoService.list(carInfo);
        return Result.success(new PageInfo<>(list));
    }
     @RequestMapping("add")
     public Result add(@RequestBody CarInfo carInfo){
         boolean b= carInfoService.add(carInfo);
         return b?Result.success():Result.fail();
     }

    @RequestMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        List<CarInfo> list=carInfoService.lists();
        String fileName = CarInfo.class.getSimpleName();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), CarInfo.class).sheet("模板").doWrite(list);
    }

    @RequestMapping("upload")
    public Map<String,Object> upload(MultipartFile file) throws IOException {
        HashMap<String, Object> map = new HashMap<>();
        try {
            map.put("code",200);
            EasyExcel.read(file.getInputStream(), CarInfo.class, new CarInfoListener(carInfoService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",500);
        }
        return map;
    }
}
